﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JuniorProject.Models;

namespace JuniorProject.Controllers
{
    public class SearchController : Controller
    {
        private OIT_Junior_ProjectEntities db = new OIT_Junior_ProjectEntities();

        #region SEARCH
        // GET: /Search/
        public ActionResult Index()
        {
            return RedirectToAction("SearchJobs");
        }

        public ActionResult SearchJobs(string searchStr)
        {
            var jobpostings = (from q in db.JobPostings where q.StatusID == 19 select q).ToList();

            List<SearchModel> searchModels = new List<SearchModel>();
            if (jobpostings != null)
            {
                searchModels = jobpostings.ConvertAll(new Converter<JobPostings, SearchModel>(DbSearchToViewSearch));
            }
            
            if (!String.IsNullOrEmpty(searchStr))
            {
                var searchModels2 = searchModels.Where(s => s.Title.Contains(searchStr));
                return View(searchModels2);
            }

            return View(searchModels);
        }

        // GET: /Search/Details/5
        public ActionResult Details(int id = 0)
        {
            JobPostings tempPosting = db.JobPostings.Find(id);
            SearchModel searchModel = new SearchModel(tempPosting.JobID, tempPosting.ID, tempPosting.Jobs.Title,
                                    tempPosting.Jobs.Description, tempPosting.Locations.City,
                                    tempPosting.InsertDate, tempPosting.ModifiedDate);

            return View(searchModel);
        }
        #endregion

        #region HELPERS
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private static SearchModel DbSearchToViewSearch(JobPostings db)
        {
            return new SearchModel(db.JobID, db.ID, db.Jobs.Title, db.Jobs.Description, db.Locations.City,
                                    db.InsertDate, db.ModifiedDate);
        }
        #endregion
    }
}